import { _decorator, CacheMode, Camera, Component, EventTouch, Node, RigidBody2D, UITransform, v2, v3, Vec2 } from 'cc';
import { gameMgr } from './GameMgr';
const { ccclass, property } = _decorator;

@ccclass('TouchMove')
export class TouchMove extends Component {

    @property(Node)
    player:Node;
    @property(Camera)
    cameraNode:Camera;

    tempPos=v3();
    touchStartPos=v3(0,0,0);
    direction;
    moveSpeed=6

    static ins:TouchMove=null;
    protected onLoad(): void {
        TouchMove.ins=this;
    }
    protected start(): void {
        this.node.on(Node.EventType.TOUCH_START, this.touchStartFunc, this)
        this.node.on(Node.EventType.TOUCH_MOVE, this.touchMoveFunc, this)

    }

    touchStartFunc(event:EventTouch) {
        let touchPos=event.getUILocation();
        let canvasTouchPos=this.node.getComponent(UITransform).convertToNodeSpaceAR(v3(touchPos.x,touchPos.y,0))
        this.touchStartPos=canvasTouchPos;


    }

    touchMoveFunc(event: EventTouch) {
        let movePos = this.node.getComponent(UITransform).convertToNodeSpaceAR(v3(event.getUILocation().x, event.getUILocation().y, 0))
        let ox = this.touchStartPos.x-movePos.x ;
        let oy = this.touchStartPos.y-movePos.y ;
        
         this.direction=v2(ox,oy).normalize()
   
    }

    protected update(dt: number): void {
        if(this.direction){
            this.player.setPosition(this.player.getPosition().x-this.direction.x*this.moveSpeed,this.player.getPosition().y-this.direction.y*this.moveSpeed)
            if (Math.abs(this.player.position.x)>gameMgr.ins.mapBoundary.x) {
                if (this.player.position.x>0) {
                this.player.setPosition(gameMgr.ins.mapBoundary.x,this.player.position.y)
                    
                }
                else{
                this.player.setPosition(-gameMgr.ins.mapBoundary.x,this.player.position.y)
                }
            }

            if (Math.abs(this.player.position.y)>gameMgr.ins.mapBoundary.y) {
                if (this.player.position.y>0) {
                this.player.setPosition(this.player.position.x,gameMgr.ins.mapBoundary.y)
                    
                }
                else{
                this.player.setPosition(this.player.position.x,-gameMgr.ins.mapBoundary.y)
                }
            }

            this.cameraNode.node.setPosition(this.player.getPosition())

            this.lookAtTarget(v2(this.player.getPosition().x-this.direction.x*this.moveSpeed,this.player.getPosition().y-this.direction.y*this.moveSpeed),this.player,20)
        }
     
    }


    trackTarget(targetX: number, targetY: number, ownX: number, ownY: number, speed: number = 1) {

        let deltax: number = targetX - ownX;

        let deltay: number = targetY - ownY;

        if (deltax == 0) {

            if (targetY >= ownY) {

                deltax = 0.0000001;
            } else {
                deltax = -0.0000001;
            }
        }

        if (deltay == 0) {

            if (targetX >= ownX) {

                deltay = 0.0000001;
            } else {

                deltay = -0.0000001;
            }
        }

        let angle: number = 0;
        // cc_log(deltax + "===" + deltay)
        if (deltax > 0 && deltay > 0) {

            angle = Math.atan(Math.abs(deltay / deltax));       //第一项限
        } else if (deltax < 0 && deltay > 0) {

            angle = Math.PI - Math.atan(Math.abs(deltay / deltax));   //第二项限
        } else if (deltax < 0 && deltay < 0) {

            angle = Math.PI + Math.atan(Math.abs(deltay / deltax))    //第三项限
        } else {

            angle = 2 * Math.PI - Math.atan(Math.abs(deltay / deltax))         // 第四项限
        }

        return [speed * (Math.cos(angle)), speed * (Math.sin(angle)), angle];
    }

    //面向敌人
    lookAtTarget(targetPos, selfNode, speed) {

        if (!targetPos) {
            return
        }

        let speedArr = this.trackTarget(targetPos.x, targetPos.y, selfNode.position.x, selfNode.position.y, speed);

        selfNode.angle = speedArr[2] * (180 / Math.PI);

    }

}


